//
//  AGFaceView.m
//  AGEmojiKeyboardSample
//
//  Created by zou shigang on 14/9/28.
//  Copyright (c) 2014年 Ayush. All rights reserved.
//

#import "AGFaceView.h"

@interface AGFaceView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) UICollectionView * view_collection;

@property(nonatomic, strong) NSDictionary * emojis;
@property(nonatomic, strong) UIPageControl * pageControl;

@end

@implementation AGFaceView

- (NSDictionary *)emojis {
    if (!_emojis) {
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"EmojisList"
                                                              ofType:@"plist"];
        _emojis = [[NSDictionary dictionaryWithContentsOfFile:plistPath] copy];
        NSLog(@"File read");
    }
    return _emojis;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self getFaceCollectionView];
    }
    return self;
}

#pragma mark -
#pragma mark UICollectionView
- (void)getFaceCollectionView
{
    if (self.view_collection == nil) {
        UICollectionViewFlowLayout * layout= [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        [layout setItemSize:CGSizeMake(63, 45)]; //设置每个cell显示数据的宽和高必须
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing=1;
        layout.minimumInteritemSpacing=1;
        
        CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height-44);
        self.view_collection = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
        self.view_collection.backgroundColor=[UIColor whiteColor];
        self.view_collection.delegate=self;
        self.view_collection.dataSource=self;
        self.view_collection.bounces=YES;
        self.view_collection.pagingEnabled=YES;
        self.view_collection.showsHorizontalScrollIndicator=NO;
        self.view_collection.showsVerticalScrollIndicator=NO;
        [self.view_collection registerClass:[SGFaceCell class] forCellWithReuseIdentifier:@"SGFaceCell"];
        
        [self addSubview:self.view_collection];
    }
    
    if (self.pageControl == nil) {
        CGRect rect = CGRectMake(0, self.bounds.size.height-44, self.bounds.size.width, 44);
        self.pageControl = [[UIPageControl alloc] initWithFrame:rect];
        self.pageControl.hidesForSinglePage = YES;
        self.pageControl.currentPage = 0;
        self.pageControl.backgroundColor = [UIColor clearColor];
        self.pageControl.numberOfPages=13;
        self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
        
        [self addSubview:self.pageControl];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat xOffset = self.view_collection.contentOffset.x;
    NSInteger width = self.bounds.size.width;
    NSInteger se = xOffset/width;
    
    if (se >= 11) {
        if (xOffset > width*11) {
            se=12;
        }
    }
    
    self.pageControl.currentPage=se;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark -
#pragma mark UICollectionViewDataSource, UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
    NSArray * array_face = [[self emojis] objectForKey:@"People"];
    return [array_face count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    SGFaceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SGFaceCell" forIndexPath:indexPath];
    
    // load the image for this cell
    //cell.image_thumb.image=[UIImage imageNamed:[array_face objectAtIndex:indexPath.row]];
    NSArray * array_face = [[self emojis] objectForKey:@"People"];
    NSInteger index = indexPath.row;
    if (index < [array_face count]) {
        NSString * str_face = [array_face objectAtIndex:index];
        cell.label_title.text=str_face;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
    NSArray * array_face = [[self emojis] objectForKey:@"People"];
    NSInteger index = indexPath.row;
    
    if (index < [array_face count]) {
        if (self.delegate_emjo) {
            [self.delegate_emjo doClickIndex:[array_face objectAtIndex:index]];
        }
    }
}

@end


@implementation SGFaceCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        
        self.label_title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 63, 45)];
        self.label_title.font=[UIFont systemFontOfSize:40];
        self.label_title.textAlignment=NSTextAlignmentCenter;
        [self addSubview:self.label_title];
    }
    return self;
}

@end

















